package com.xiao.controller;

import com.xiao.domain.ResponseResult;
import com.xiao.domain.dto.AddArticleDto;
import com.xiao.domain.dto.article.QueryArticleDto;
import com.xiao.domain.dto.article.SaveUpdateArticleDto;
import com.xiao.domain.vo.PageVo;
import com.xiao.domain.vo.article.UpdateArticleVo;
import com.xiao.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/content/article")
@Api(tags = "文章")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @PostMapping
    @ApiOperation("新增博文")
    @ApiImplicitParam(name = "addArticleDto",value = "新增文本 DTO")
    public ResponseResult addArticle(@RequestBody AddArticleDto addArticleDto){
        ResponseResult responseResult = articleService.addArticle(addArticleDto);
        return  responseResult;

    }

    @GetMapping("/list")
    @ApiOperation("返回文章分页列表，支持模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小"),
            @ApiImplicitParam(name = "dto",value = "查询接受参数-dto"),
    })
    public ResponseResult<PageVo> getArticleList(Integer pageNum, Integer pageSize, QueryArticleDto dto){
        ResponseResult<PageVo> responseResult = articleService.getArticleList(pageNum, pageSize, dto);
        return responseResult;
    }

    @GetMapping("/{id}")
    @ApiOperation("修改文章-根据文章ID返回文章信息,包含所关联的标签列表")
    @ApiImplicitParam(name = "id",value = "文章id")
    public ResponseResult<UpdateArticleVo> updateArticle(@PathVariable("id") Long id){
        ResponseResult<UpdateArticleVo> responseResult = articleService.updateArticle(id);
        return responseResult;
    }


    @PutMapping
    @ApiOperation("保存修改好的文章")
    @ApiImplicitParam(name = "dto",value = "请求保存修改文章的dto")
    public ResponseResult saveUpdateArticle(@RequestBody SaveUpdateArticleDto dto){
        ResponseResult responseResult = articleService.saveUpdateArticle(dto);

        return responseResult;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据文章id列表，删除文章")
    @ApiImplicitParam(name = "idList",value = "文章id列表")
    public ResponseResult delArticleByIdList(@PathVariable("id") List<Long> idList){
        ResponseResult responseResult = articleService.delArticleByIdList(idList);
        return responseResult;
    }


}
